package com.kds.smarthome.service;

import com.kds.smarthome.entity.Building;
import com.kds.smarthome.common.EnergyType;
import com.kds.smarthome.mapper.BuildingMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

@Service
@Transactional
public class BuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    public List<Building> findAll() {
        return buildingMapper.findAll();
    }

    public Building findById(Integer tid) {
        return buildingMapper.findById(tid);
    }

    public Building findByCode(String code) {
        return buildingMapper.findByCode(code);
    }

    public void createBuilding(Building building) {
        buildingMapper.insert(building);
        
        if (building.getEnergyTypes() != null && !building.getEnergyTypes().isEmpty()) {
            for (EnergyType energyType : building.getEnergyTypes()) {
                buildingMapper.insertBuildingEnergy(building.getCode(), energyType);
            }
        }
    }

    public void updateBuilding(Building building) {
        buildingMapper.update(building);
        
        buildingMapper.deleteAllBuildingEnergies(building.getCode());
        if (building.getEnergyTypes() != null && !building.getEnergyTypes().isEmpty()) {
            for (EnergyType energyType : building.getEnergyTypes()) {
                buildingMapper.insertBuildingEnergy(building.getCode(), energyType);
            }
        }
    }

    public void deleteBuilding(Integer tid) {
        buildingMapper.delete(tid);
    }

    public List<Building> findByUsername(String username) {
        return buildingMapper.findByUsername(username);
    }

    public void assignBuildingToUser(String username, String buildingCode) {
        buildingMapper.insertUserBuilding(username, buildingCode);
    }

    public void removeBuildingFromUser(String username, String buildingCode) {
        buildingMapper.deleteUserBuilding(username, buildingCode);
    }

    public List<String> findUsersByBuildingCode(String buildingCode) {
        return buildingMapper.findUsersByBuildingCode(buildingCode);
    }

    public void addBuildingEnergyType(String buildingCode, EnergyType energyType) {
        buildingMapper.insertBuildingEnergy(buildingCode, energyType);
    }

    public void removeBuildingEnergyType(String buildingCode, EnergyType energyType) {
        buildingMapper.deleteBuildingEnergy(buildingCode, energyType);
    }

    public List<EnergyType> getBuildingEnergyTypes(String buildingCode) {
        return buildingMapper.findEnergyTypesByBuildingCode(buildingCode);
    }

    public void setBuildingEnergyTypes(String buildingCode, Set<EnergyType> energyTypes) {
        buildingMapper.deleteAllBuildingEnergies(buildingCode);
        
        if (energyTypes != null && !energyTypes.isEmpty()) {
            for (EnergyType energyType : energyTypes) {
                buildingMapper.insertBuildingEnergy(buildingCode, energyType);
            }
        }
    }

}
